package io.sage.modules.flow.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.sage.modules.flow.service.FlowCustomersService;
import io.sage.modules.flow.service.ReadsenseApiService;
import io.sage.modules.readsenseapi.entity.BFlowCustomers;
import io.sage.modules.readsenseapi.vo.FlowCustomersVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("flowCustomersService")
public class FlowCustomersServiceImpl implements FlowCustomersService {
    @Autowired
    private ReadsenseApiService readsenseApiService;

    @Override
    public List<BFlowCustomers> selectFlowCustomersList(Map<String, Object> params) {
        String uri = "company/customers?customer_group_id=";
        Map<String, String> json = readsenseApiService.gets(uri);
        JSONArray jsonArray = JSONArray.parseArray(json.get("entity"));
        List<BFlowCustomers> bFlowCustomers = jsonArray.toJavaList(BFlowCustomers.class);
        return bFlowCustomers;
    }

    @Override
    public int deleteBatchIds(String[] ids) {
        String id = "";
        for (int i = 0; i < ids.length; i++) {
            id = ids[i];
            String uri = "company/customers/" + id;
            System.out.println("id==>" + id);
            String getsagin = readsenseApiService.deletes(uri);
        }
        return 1;
    }

    /**
     * 顾客的通过id查找
     * @param id
     * @return
     */
    @Override
    public BFlowCustomers selectByPrimaryKey(String id) {
        String uri = "company/customers/" + id;
        String json = readsenseApiService.getsagin(uri);
        //jaon字符串转换为实体类
        BFlowCustomers bFlowCustomers = JSONObject.toJavaObject(JSONObject.parseObject(json), BFlowCustomers.class);
        return bFlowCustomers;
    }

    @Override
    public int updateByPrimaryKeySelective(FlowCustomersVo flowCustomersVo) {
        String s = flowCustomersVo.toString();
        String uri = "company/customers?" + s;
        String postsa = readsenseApiService.postsagin(uri);
        return 1;
    }

    @Override
    public int insert(FlowCustomersVo flowCustomersVo) {
        String s = flowCustomersVo.toString();
        System.out.println("s==>"+s);
        String uri = "company/customers?" + s;
        String postsa = readsenseApiService.postsagin(uri);
        return 1;
    }
}
